<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="角色名"
        style="width: 160px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="dialogAddVisible = true"
      >
        添加
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      max-height="500px"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        type="index"
        min-width="15%"
        align="center"
      />
      <el-table-column label="名称" min-width="30%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" min-width="40%" align="center">
        <template slot-scope="{row}">
          <span>{{ row.detail }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="15%" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button :disabled="row.name === 'admin' || row.name === 'ms'" size="mini" type="success" @click="confirmDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <el-dialog :visible.sync="dialogAddVisible" title="添加权限" @close="handleDialogAddClose">
      <el-form ref="formAdd" :model="formAdd" :rules="formAddRules" label-width="80px">
        <el-form-item label="权限名" prop="name">
          <el-input v-model="formAdd.name" placeholder="请输入3-10个英文字符" />
        </el-form-item>
        <el-form-item label="权限描述" prop="detail">
          <el-input v-model="formAdd.detail" placeholder="请输入权限描述" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitFormAdd">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { fetchList, addRole, deleteRole } from '@/api/role'
import { Message } from 'element-ui'

export default {
  name: 'Role',
  components: { Pagination },
  data() {
    return {
      dialogAddVisible: false,
      listLoading: false,
      total: 0,
      list: [],
      listQuery: {
        name: '',
        page: 1,
        limit: 10
      },
      formAdd: {
        name: '',
        detail: ''
      },
      formAddRules: {
        name: [
          { required: true, pattern: /^[a-zA-Z]{3,10}$/, message: '请输入3-10个英文字符', trigger: ['change', 'blur'] }
        ],
        detail: [
          { required: true, message: '描述不能为空', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  created() { this.getList() },
  methods: {
    getList() {
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() { this.getList() },
    handleDialogAddClose() {
      this.$refs['formAdd'].resetFields()
    },
    submitFormAdd() {
      this.$refs['formAdd'].validate((valid) => {
        console.log(this.formAdd)
        if (valid) {
          addRole(this.formAdd).then(response => {
            Message.success(response.data)
            this.dialogAddVisible = false
            this.getList()
          })
        }
      })
    },
    confirmDelete(row) {
      this.$confirm(`此操作将永久删除该角色"${row.name}"及用户关联关系, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRole(row.id).then(response => {
          this.getList()
          Message.success(response.data)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
